package com.gitee.llj.api.service.sys.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.gitee.llj.api.mapper.sys.SysModuleMapper;
import com.gitee.llj.api.service.sys.ISysGroupModuleService;
import com.gitee.llj.api.service.sys.ISysModuleService;
import com.gitee.llj.api.service.sys.ISysUserGroupService;
import com.gitee.llj.api.service.sys.ISysUserService;
import com.gitee.llj.entity.sys.SysModule;
import com.gitee.llj.entity.sys.vo.SysModuleVO;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 模块表 服务实现类
 * </p>
 *
 * @author llj
 * @since 2018-03-16
 */
@Service
public class SysModuleService extends ServiceImpl<SysModuleMapper, SysModule> implements ISysModuleService {

    @Resource
    private SysModuleMapper sysModuleMapper;

    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysUserGroupService sysUserGroupService;
    @Resource
    private ISysGroupModuleService sysGroupModuleService;

    @Override
    public List<SysModuleVO> selectAllSysModules() {
        List<SysModule> parents = selectOneLevelSysModules();
        List<SysModuleVO> parentVOs = new ArrayList<>();
        EntityWrapper<SysModule> ew = new EntityWrapper<>();
        ew.eq("deleted", "0");
        ew.eq("module_type", "0");
        parents.forEach(item -> {
            SysModuleVO sysModuleVO = (SysModuleVO) item;
            //查询子菜单
            ew.eq("pid", item.getId());
            sysModuleVO.setSubModules(this.selectList(ew));
            parentVOs.add(sysModuleVO);
        });
        return parentVOs;
    }

    @Override
    public List<SysModule> selectOneLevelSysModules() {
        return sysModuleMapper.selectOneLevelSysModules();
    }

    @Override
    public List<SysModule> selectSysModuelsByUser(String userId) {
        if (StringUtils.isNotBlank(userId)) {
            return sysModuleMapper.selectSysModuelsByUser(userId);
        }
        return null;
    }

    @Override
    public List<SysModule> selectPermissionByUser(String userId) {
        if (StringUtils.isNotBlank(userId)) {
            return sysModuleMapper.selectPermissionByUser(userId);
        }
        return null;
    }
}
